package org.hackystat.sdsa.analyzer.model.activity;

import java.util.Map;
import java.util.logging.Logger;

import jess.Fact;
import jess.JessException;
import jess.RU;
import jess.Rete;
import jess.Value;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;


/**
 * Implement buffer transition action.
 * 
 * @author Hongbing Kou
 */
public class BufferTransActivity  extends Activity {
  /** From resource. */
  private String fromResourceUri;
  /** From buffer name. */
  public static final String FROM_BUFF_NAME = "from-buff-name";
  /** To buffer name. */
  public static final String TO_BUFF_NAME = "to-buff-name";
  /** Activity Type */
  private static final String BUFFERTRANS_TYPE = "BUFFTRANS";
  
  /**
   * Constructs an empty buffer trans activity.
   */
  public BufferTransActivity() {
    super.setType(BUFFERTRANS_TYPE);
  }
  
  /**
   * Instantiates an instance.
   * 
   * @param sensorData Sensor data.
   * @param logger Service logger.
   */
  public BufferTransActivity(SensorData sensorData, Logger logger) {
    super(sensorData, logger);
    
    super.setType(BUFFERTRANS_TYPE);
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    String fromBuff = propMap.get(FROM_BUFF_NAME);
    this.fromResourceUri = fromBuff;
  }

  /**
   * Sets the from resource.
   * 
   * @param fromResource The from resource. 
   */
  public void setFromResource(String fromResource) {
    this.fromResourceUri = fromResource;
  }

  /**
   * Gets the from buffer.
   * 
   * @return From resource. 
   */
  public String getFromResource() {
    return this.fromResourceUri;
  }

  /** Get the representation of this activity. 
   * 
   * @return String representation of this activity.
   */
  @Override public String getRepresentation() {
    String fromFile = super.extractFileName(this.fromResourceUri);
    return "FROM=" + fromFile;
  }
  
  /**
   * Gets the file of the resource URI.
   * 
   * @return To file.
   */
  public String getToFile() {
    return super.extractFileName(super.getResourceUri());
  }
  
  /**
   * Makes Jess facts in the given Jess rete engine.
   * 
   * @param index Action index in episode. 
   * @param engine Jess rete engine.
   * @throws JessException If error while constructing jess action.
   * @return Jess fact of this action.
   */
  @Override public Fact assertActivity(int index, Rete engine) throws JessException  {
    Fact f = new Fact("BufferTransActivity", engine);
    f.setSlotValue(INDEX_SLOT, new Value(index, RU.INTEGER));
    f.setSlotValue(FILE_SLOT, new Value(this.getToFile(), RU.STRING));
    f.setSlotValue("leavingFile", new Value(this.getFromResource(), RU.STRING));
    Fact assertedFact = engine.assertFact(f);
    return assertedFact;
  }
  
  /**
   * Gets buff trans action string.
   * 
   * @return Buff trans action. 
   */
  @Override public String toString() {
    StringBuffer buff = new StringBuffer(30);
    buff.append(super.toString()).append(" {");
    buff.append(this.getRepresentation()).append('}');
        
    return  buff.toString();
  }

}
